package com.yy.micro.magic.core.loader.impl;

import com.yy.micro.magic.core.context.ResourceManager;
import com.yy.micro.magic.core.model.PlanSchedule;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
 /**
   * @author YY
   * @version 1.0.0
   * @update 2021/9/22 9:24 下午
   * @description: 数据变换计划{@link PlanSchedule}的加载器。默认加载目录："/magic-cube/schedules/"，
  * 文件后缀："*.json"，可通过配置调整。如：
  * {
  *   "planCode":"test",
  *   "version": 1.0,
  *   "rules": [
  *
  *     {
  *       "key": "dddd#jj-1#ccc",
  *       "name": "ccc",
  *       "strategy": "copy",
  *       "input": "aaaa#gg-0#ccc",
  *       "output": "",
  *       "dynamic": true
  *     },
  *     {
  *       "key": "dddd#jj-1#code",
  *       "name": "code",
  *       "strategy": "codeMap",
  *       "input": "aaaa#gg-0#code",
  *       "format": "test",
  *       "dynamic": true
  *     },
  *     {
  *       "key": "dddd#dy-1#name",
  *       "name": "name",
  *       "strategy": "copy",
  *       "input": "aaaa#dynamic-0#name",
  *       "dynamic": true
  *     },
  *     {
  *       "key": "dddd#dy-1#age",
  *       "name": "age",
  *       "strategy": "copy",
  *       "input": "aaaa#dynamic-0#age",
  *       "dynamic": true
  *     },
  *     {
  *       "key": "dddd#mc-0#test-0#from",
  *       "name": "from",
  *       "strategy": "copy",
  *       "input": "aaaa#gg-0#magic-0#from",
  *       "dynamic": true,
  *       "syncParent": true
  *     },
  *     {
  *       "key": "dddd#mc-0#test-0#to",
  *       "name": "to",
  *       "strategy": "copy",
  *       "input": "aaaa#gg-0#magic-0#to",
  *       "dynamic": true,
  *       "syncParent": true
  *     }
  *   ]
  * }
 */
@Component
public class PlanScheduleResourceLoader extends AbstractResourceLoader<PlanSchedule> {


    @Value("${spring.micro.map.schedule_path:/magic-cube/schedules/}")
    private String scheduleBasePath;

    @Value("${spring.micro.map.schedule_postfix:*.json}")
    private String schedulePostfix;
    @Autowired
    private ResourceManager manager;
    @Override
    protected void handle(final List<PlanSchedule> resources) {
        if (null != resources && !resources.isEmpty()){
            Map<String,Double> planVersions = new HashMap<>();
            resources.forEach(r->{
                String planCode = r.getPlanCode();
                if(planVersions.containsKey(planCode)){
                    double version = r.getVersion();
                    Double v = planVersions.get(planCode);
                    if (version > v){
                        manager.set(getClass(),planCode,r);
                    }
                }else {
                    manager.set(getClass(),r.getPlanCode(),r);
                }
            });
        }
    }

    @Override
    protected Class<PlanSchedule> getResourceClass() {
        return PlanSchedule.class;
    }

    @Override
    protected String getResourcePath() {
        return scheduleBasePath;
    }

    @Override
    protected String getResourcePost() {
        return schedulePostfix;
    }
}
